<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      v-model:page="page"
      :permission="permissionList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu="scope">
        <el-button
          type="primary"
          text
          icon="top"
          @click.stop="handleSuggest(scope.row)"
          v-show="scope.row.recommendSort < 1"
          >推荐
        </el-button>
        <el-button
          type="primary"
          text
          icon="bottom"
          @click.stop="handleCancelSuggest(scope.row)"
          v-show="scope.row.recommendSort >= 1"
          >取消推荐
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
  <el-dialog title="推荐" v-model="dialogVisible" width="800px" :before-close="handleClose">
    <el-form :model="recordRow" ref="form" label-width="80px">
      <el-form-item
        label="推荐值："
        prop="recommendSort"
        :rules="[{ required: true, message: '推荐值不能为空' }]"
      >
        <el-input-number v-model="recordRow.recommendSort" :min="1" :max="100" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="reLoadList">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// import { projectList, remove } from '@/api/report/report';
import { projectList, recommend } from '@/api/project/suggest';
import { mapGetters } from 'vuex';

export default {
  data() {
    return {
      form: {},
      selectionList: [],
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        indexLabel: '序号',
        indexWidth: '70',
        height: 'auto',
        calcHeight: 32,
        tip: false,
        searchShow: true,
        searchMenuSpan: 3,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        dialogClickModal: false,
        addBtn: false,
        header: false,
        delBtn: false,
        column: [
          {
            label: '项目名称',
            prop: 'projectName',
            search: true,
            slot: true,
          },
          {
            label: '发起人',
            prop: 'creator',
          },
          {
            label: '项目地址',
            prop: 'projectAddr',
          },

          {
            label: '项目有效开始时间',
            prop: 'projectStartTime',
          },
        {
            label: '项目类型',
            prop: 'projectType',
            search: false,
            type: 'select',
            props: {
              label: 'name',
              value: 'value',
            },
            dicUrl: '/blade-system/dict/child-list?current=1&size=10&parentId=1894952129912856578', // 假设这是获取字典的URL
            dicMethod: 'get',
            dicFormatter: res => {
              return res.data.map(item => {
                return {
                  name: item.dictValue,
                  value: item.dictKey,
                };
              });
            },
          },
          {
            label: '项目描述',
            prop: 'projectRemark',
          },
          {
            label: '推荐值',
            prop: 'recommendSort',
            formatter: (row, column, cellValue) => {
              if (cellValue === -1) {
                return '-';
              } else {
                return cellValue;
              }
            },
          },
        ],
      },
      data: [],
      dialogVisible: false,
      recordRow: {},
      form: {},
    };
  },
  computed: {
    ...mapGetters(['userInfo', 'permission']),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: false,
        delBtn: true,
        editBtn: false,
      };
    },
  },
  methods: {
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          let params = { ...this.recordRow };
          recommend(params).then(
            () => {
              this.onLoad(this.page);
              this.$message({
                type: 'success',
                message: '操作成功!',
              });
              this.dialogVisible = false;
            },
            error => {
              console.log(error);
            }
          );
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.recordRow = {};
    },
    /**
     * 推荐
     * @param {*} row
     */
    handleSuggest(row) {
      this.dialogVisible = true;
      this.recordRow = row;
    },
    /**
     * 取消推荐
     * @param {*} row
     */
    handleCancelSuggest(row) {
      let params = { ...row, recommendSort: -1 };
      recommend(params).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.dialogVisible = false;
        },
        error => {
          console.log(error);
        }
      );
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    reLoadList() {
      this.dialogVisible = false;
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params = { ...params, status: 2 };
      projectList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>

<style></style>
